package com.ruyuan.engine.datagen;
import com.alibaba.fastjson.JSON;
import com.ruyuan.engine.pojo.ClientLog;
import com.ruyuan.engine.utils.Constants;
import com.ruyuan.engine.utils.KafkaProducerUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
 *  生成client_log数据
 */
public class MockAppLogData {
    static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
    static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss");

    public static void main(String[] args) throws InterruptedException {
        mockData();
    }

    private static void mockData() {
        // 构造一个kafka生产者客户端
        Producer<String, String> kafkaProducer = KafkaProducerUtils.getProducer();
        while (true) {
            ClientLog clientLog = getClientLogOneMessage();
//            clientLog.setEventId("M");
            String log = JSON.toJSONString(clientLog);
            System.out.println(log);
            ProducerRecord<String, String> record = new ProducerRecord<>(Constants.CLIENT_LOG, log);
            kafkaProducer.send(record);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 发送重复数据
     * */
    public static ClientLog getClientLogOneMessage() {
        ClientLog clientLog = new ClientLog();
        String userID = MockDataUtils.userId();
        clientLog.setUserNo("2A7eGfG1");
        clientLog.setUserName("张三");
        clientLog.setAppId(MockDataUtils.getAppID());
        clientLog.setAddr(MockDataUtils.cities[MockDataUtils.getRandNum(9, 0)] + MockDataUtils.getRoad());
        clientLog.setAppVersion(MockDataUtils.getAppVersionID());
        clientLog.setCarrier(MockDataUtils.getCarrier());
        clientLog.setImei(userID);
        clientLog.setDeviceType(MockDataUtils.getDeviceType());
        clientLog.setIp(MockDataUtils.getRandomIp());
        clientLog.setNetType(MockDataUtils.network());
        clientLog.setOsName(MockDataUtils.getOsNameType());
        clientLog.setOsVersion(MockDataUtils.getOsVersion());
        clientLog.setSessionId(MockDataUtils.getRandomNumAndCharacters(16));
        clientLog.setDetailTime(String.valueOf(MockDataUtils.randomDateDetail("20190217000000", "20210217000000", sdf2)));
        clientLog.setCreateTime(System.currentTimeMillis());
        clientLog.setGps(MockDataUtils.randomLonLat(3.51, 53.33, 73.33, 135.05, "Lon+lat"));
        clientLog.setEventId(RandomStringUtils.randomAlphabetic(1).toUpperCase());
        clientLog.setEventType("pay");
        setTagAttribute(clientLog);
        return clientLog;
    }

    public static Iterable<ClientLog> dataGenClientLogIterator() {
        List<ClientLog> clientLogList = new ArrayList();
        ClientLog clientLogOneMessage1 = getClientLogOneMessage();
        clientLogOneMessage1.setUserNo("862C6Edg");
        clientLogOneMessage1.setEventId("A");
        ClientLog clientLogOneMessage2 = getClientLogOneMessage();
        clientLogOneMessage2.setUserNo("89768143");
        clientLogOneMessage2.setEventId("M");
        ClientLog clientLogOneMessage3 = getClientLogOneMessage();
        clientLogOneMessage3.setUserNo("8D4Dg5eC");
        clientLogOneMessage3.setEventId("S");
        ClientLog clientLogOneMessage4 = getClientLogOneMessage();
        clientLogOneMessage4.setUserNo("8cDf538e");
        clientLogList.add(clientLogOneMessage1);
        clientLogList.add(clientLogOneMessage2);
        clientLogList.add(clientLogOneMessage3);
        clientLogList.add(clientLogOneMessage4);
        return clientLogList;
    }



    private static void setTagAttribute(ClientLog clientLog) {
        HashMap attributes = new HashMap();
        attributes.put("tagCondition1","tagValue1");
        attributes.put("tagCondition2","tagValue2");
        clientLog.setProperties(attributes);
    }

    /**
     * 生成新的数据
     * */
    private static ClientLog getClientLog() {
        ClientLog clientLog = new ClientLog();
        String userID = MockDataUtils.userId();
        clientLog.setUserNo(MockDataUtils.userId());
        clientLog.setUserName(MockDataUtils.getChineseName());
        clientLog.setAppId(MockDataUtils.getAppID());
        clientLog.setAddr(MockDataUtils.cities[MockDataUtils.getRandNum(9, 0)] + MockDataUtils.getRoad());
        clientLog.setAppVersion(MockDataUtils.getAppVersionID());
        clientLog.setCarrier(MockDataUtils.getCarrier());
        clientLog.setImei(userID);
        clientLog.setDeviceType(MockDataUtils.getDeviceType());
        clientLog.setIp(MockDataUtils.getRandomIp());
        clientLog.setNetType(MockDataUtils.network());
        clientLog.setOsName(MockDataUtils.getOsNameType());
        clientLog.setOsVersion(MockDataUtils.getOsVersion());
        clientLog.setSessionId(MockDataUtils.getRandomNumAndCharacters(16));
        // 时间
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss");
        //创建详细时间
        clientLog.setDetailTime(String.valueOf(MockDataUtils.randomDateDetail("20190217000000", "20210217000000", sdf2)));
        //创建日期
        clientLog.setCreateTime(System.currentTimeMillis());
        clientLog.setGps(MockDataUtils.randomLonLat(3.51, 53.33, 73.33, 135.05, "Lon+lat"));
        clientLog.setEventId(RandomStringUtils.randomAlphabetic(1).toUpperCase());
        clientLog.setEventType("login");
        return clientLog;
    }
}
